package com.cheer.ba.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.OilCardInfoModel;
import com.cheer.ba.presenter.impl.OilMealPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.widget.ShowPopupView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 柴油卡详情界面
 */
public class OilCardDetailAty extends BaseCommAty implements ShowPopupView.OnItemPopupClick {

    @BindView(R.id.id_detail_root_view)
    RelativeLayout mRelaRootView;
    //logo
    @BindView(R.id.id_oil_logo_img)
    ImageView mImgLogo;
    //油卡名称
    @BindView(R.id.id_oil_name_txt)
    TextView mTxtCardName;
    //油卡类别
    @BindView(R.id.id_oil_card_txt)
    TextView mTxtCardType;
    //油卡卡号
    @BindView(R.id.id_oil_card_num_txt)
    TextView mTxtCardNum;
    //激活状态
    @BindView(R.id.id_activity_txt)
    TextView mTxtCardActivity;

    //司机信息
    @BindView(R.id.id_driver_title_txt)
    TextView mTxtDriverTitle;
    //司机是否移除
    @BindView(R.id.id_driver_cancel_txt)
    TextView mTxtDriverCancel;
    //司机车牌号
    @BindView(R.id.id_driver_num_txt)
    TextView mTxtDriverNum;
    //司机名称
    @BindView(R.id.id_driver_name_txt)
    TextView mTxtDriverName;
    //司机联系电话
    @BindView(R.id.id_driver_phone_txt)
    TextView mTxtDriverPhone;

    //记录司机手机号
    private String mSaveDriverTel;

    private OilCardInfoModel mOilCardModel;

    //弹出头像框
    private ShowPopupView popupView;
    public static final String[] POPUP_ITEMS = {"移除油卡"};

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_oil_card_details, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new OilMealPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText("柴油卡详情");
        mOilCardModel = (OilCardInfoModel) getIntent().getSerializableExtra(IntentKey.General.KEY_MODEL);
        if (null == mOilCardModel) {
            dataStatus(2);
            return;
        }
        if (mOilCardModel.getCardType() == 1) {
            mTxtCardName.setText("中国石油（" + mOilCardModel.getProvince() + "）");
            mImgLogo.setBackgroundResource(R.drawable.ic_china_oil_logo);
        } else if (mOilCardModel.getCardType() == 2) {
            mTxtCardName.setText("中国石化（" + mOilCardModel.getProvince() + "）");
            mImgLogo.setBackgroundResource(R.drawable.ic_shihua_logo);
        } else {
            mTxtCardName.setText("中国海油（" + mOilCardModel.getProvince() + "）");
            mImgLogo.setBackgroundResource(R.drawable.ic_haiyou_logo);
        }
        if (mOilCardModel.getCardStatus() == 1) {
            //未激活
            mTxtCardActivity.setText("去激活");
            mTxtCardActivity.setBackgroundResource(R.drawable.shape_bg_000_red_kuan);
            mTxtCardActivity.setTextColor(getResources().getColor(R.color.red));
        } else {
            mTxtCardActivity.setText("余额：" + Currency.returnDollar(mOilCardModel.getBalance()));
            mTxtCardActivity.setBackgroundResource(R.color.transparent);
            mTxtCardActivity.setTextColor(getResources().getColor(R.color.white));
        }
        mTxtCardType.setText("加油啦车队卡");
        mTxtCardNum.setText(StringUtils.replaceBankString(mOilCardModel.getCardNum(), 0));

        showDriverInfo(mOilCardModel);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        popupView = new ShowPopupView(this);
        popupView.setOnItemPopupClick(this);
        setTitleIcon(R.drawable.ic_more, false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupView.showAtLocPop(mRelaRootView, POPUP_ITEMS);
            }
        });
    }

    /**
     * 展示司机信息
     */
    private void showDriverInfo(OilCardInfoModel model) {
        if (StringUtils.isEmpty(model.getPlateNumber())) {
            mTxtDriverNum.setText("车牌号:未知");
        } else {
            mTxtDriverNum.setText("车牌号:" + model.getPlateNumber());
        }
        mTxtDriverName.setText(model.getCustName());
        mTxtDriverPhone.setText(model.getCardTel());
        mSaveDriverTel = model.getCardTel();
    }

    /**
     * 处理油卡删除信息
     */
    @Override
    public void OnItemClick(int position, String tag) {
        switch (position) {
            // "移除该油卡",
            case 0:
                ((OilMealPresenter) presenter).delOilCard(mOilCardModel.getUid());
                break;
        }
    }

    @OnClick({R.id.id_driver_cancel_txt, R.id.id_activity_txt})
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_driver_cancel_txt:
                //更换司机
                Intent intent = new Intent(aty, DriversListAty.class);
                intent.putExtra(IntentKey.General.KEY_DATA, mOilCardModel.getCardNum());
                showActivity(aty, intent);
                break;
            case R.id.id_activity_txt:
                //去激活
                if (mOilCardModel.getCardStatus() == 1) {
                    TransitAty.startTransitAty(aty, IntentCode.General.JUMP_CARD_DETAIL, mOilCardModel.getUid(), "0", mOilCardModel.getCardNum());
                }
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.DEL_OIL_CARD, tag)) {
                SVProgressHUD.showInfoWithStatus(aty, "移除油卡成功");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //移除油卡成功
                        BaseEvent event = new BaseEvent();
                        event.type = IntentKey.Oil.DEL_OIL_CARD;
                        event.id = mOilCardModel.getUid();
                        EventBus.getDefault().post(event);
                        finish();
                    }
                }, 2000);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        //返回类型
        String type = event.type;
        if (TextUtils.equals(type, ConstantTag.BIND_DRIVERS)) {
            //绑定司机成功
            OilCardInfoModel model = (OilCardInfoModel) event.model;
            showDriverInfo(model);
        } else if (TextUtils.equals(type, IntentKey.Oil.OIL_ACTIVITY_SUCCESS)) {
            //激活油卡成功
            mTxtCardActivity.setBackgroundResource(R.color.transparent);
            mTxtCardActivity.setTextColor(getResources().getColor(R.color.white));
            if (null != mOilCardModel) {
                mOilCardModel.setCardStatus(2);
                String money = mOilCardModel.getBalance();
                mTxtCardActivity.setText("余额：" + Currency.returnDollar(money));
            }
        } else if (TextUtils.equals(type, ConstantTag.DEL_DRIVERS)) {
            //移除司机
            OilCardInfoModel model = (OilCardInfoModel) event.model;
            if (TextUtils.equals(model.getCardTel(), mSaveDriverTel)) {
                showDriverInfo(mOilCardModel);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
